package net.xdclass.service.impl;

import net.xdclass.controller.request.LinkGroupRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.model.vo.LinkGroupVO;
import net.xdclass.service.LinkGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LinkGroupServiceImpl implements LinkGroupService {


    @Autowired
    private LinkGroupManager linkGroupManager;

    @Override
    public int add(LinkGroupRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        if (accountNo == null) {
            return 0;
        }
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(request.getTitle());
        return linkGroupManager.add(linkGroupDO);
    }

    @Override
    public int del(Long id) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        return linkGroupManager.del(accountNo,id);
    }

    @Override
    public LinkGroupVO detail(Long id) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO  linkGroupDO = linkGroupManager.detail(id,accountNo);
        LinkGroupVO linkGroupVo = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVo);
        return linkGroupVo;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllGroup(accountNo);
        List<LinkGroupVO> groupVOList = linkGroupDOList.stream().map(obj -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(obj, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());

        return groupVOList;
    }



    @Override
    public int updateById(LinkGroupUpdateRequest request) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setId(request.getId());
        linkGroupDO.setAccountNo(accountNo);

        int rows = linkGroupManager.updateById(linkGroupDO);

        return rows;
    }


}
